package com.harveyguo.nihonngo.service.impl;

import com.harveyguo.nihonngo.model.dto.CoachDto;
import com.harveyguo.nihonngo.model.entity.Coach;
import com.harveyguo.nihonngo.repositories.CoachRepository;
import com.harveyguo.nihonngo.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 教练Service实现类
 */
@Service
public class CoachServiceImpl implements CoachService {

    @Autowired
    private CoachRepository coachRepository;

    @Override
    public CoachDto createCoach(CoachDto coachDto) {
        // TODO: 实现教练创建逻辑
        return coachDto;
    }

    @Override
    public CoachDto findById(String id) {
        Optional<Coach> coach = coachRepository.findById(id);
        if (coach.isPresent()) {
            return convertToDto(coach.get());
        }
        return null;
    }

    @Override
    public List<CoachDto> findAll() {
        return coachRepository.findAll().stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<CoachDto> findByGymId(String gymId) {
        return coachRepository.findByBoxingGymId(gymId).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<CoachDto> findByVerified(Boolean isVerified) {
        return coachRepository.findByIsVerified(isVerified).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<CoachDto> findBySpeciality(String speciality) {
        return coachRepository.findBySpecialityContaining(speciality).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<CoachDto> findNearbyCoaches(Double latitude, Double longitude, Double radius) {
        return coachRepository.findNearbyCoaches(latitude, longitude, radius).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public CoachDto updateCoach(String id, CoachDto coachDto) {
        // TODO: 实现教练更新逻辑
        return coachDto;
    }

    @Override
    public void deleteCoach(String id) {
        coachRepository.deleteById(id);
    }

    @Override
    public CoachDto verifyCoach(String id) {
        Optional<Coach> coach = coachRepository.findById(id);
        if (coach.isPresent()) {
            Coach c = coach.get();
            c.setIsVerified(true);
            return convertToDto(coachRepository.save(c));
        }
        return null;
    }

    private CoachDto convertToDto(Coach coach) {
        CoachDto dto = new CoachDto();
        dto.setId(coach.getId());
        dto.setUsername(coach.getUsername());
        dto.setNickname(coach.getNickname());
        dto.setEmail(coach.getEmail());
        dto.setPhone(coach.getPhone());
        dto.setAvatar(coach.getAvatar());
        dto.setCertification(coach.getCertification());
        dto.setSpeciality(coach.getSpeciality());
        dto.setTeachingYears(coach.getTeachingYears());
        dto.setAchievements(coach.getAchievements());
        dto.setHourlyRate(coach.getHourlyRate());
        dto.setAvailableTime(coach.getAvailableTime());
        dto.setTeachingStyle(coach.getTeachingStyle());
        dto.setIsVerified(coach.getIsVerified());
        dto.setAge(coach.getAge());
        dto.setWeight(coach.getWeight());
        dto.setHeight(coach.getHeight());
        dto.setBoxingLevel(coach.getBoxingLevel());
        dto.setExperienceYears(coach.getExperienceYears());
        dto.setPreferredStyle(coach.getPreferredStyle());
        dto.setIntroduction(coach.getIntroduction());
        dto.setCreateTime(coach.getCreateTime());
        dto.setUpdateTime(coach.getUpdateTime());

        if (coach.getBoxingGym() != null) {
            dto.setGymId(coach.getBoxingGym().getId());
            dto.setGymName(coach.getBoxingGym().getName());
        }

        return dto;
    }
}